﻿package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author hbb
	 */
	public class Layout extends Sprite
	{
		
		public function Layout() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function get input():String
		{
			return _input.text;
		}
		
		public function set clickHandler(v:Function):void
		{
			if(_clickHandler != v)
				_clickHandler = v;
		}
		
		public function set output(v:String):void
		{
			_output.appendText( v + '\n' );
			_output.scrollV = _output.maxScrollV;
		}
		
		private function init(e:Event=null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			createTitle();
			createInput();
			createButton();
			createOutput();
			draw();
		}
		
		private function draw():void
		{
			_title.x = (stage.stageWidth - _title.width) * .5;
			_title.y = stage.stageHeight * .1;
			
			var gapOfInputButton:int = 5;
			_input.x = stage.stageWidth * .5 - (_input.width + _button.width + gapOfInputButton) * .5;
			_input.y = 200;
			
			_button.x = _input.x + _input.width + gapOfInputButton;
			_button.y = _input.y;
			
			_output.x = 0;
			_output.y = stage.stageHeight - _output.height + 1;
		}
		
		private function createTitle():void
		{
			var format:TextFormat = new TextFormat();
			format.size = 48;
			
			_title = new TextField();
			_title.defaultTextFormat = format;
			_title.text = '虾米专辑资料 快速提交';
			_title.autoSize = TextFieldAutoSize.CENTER;
			
			addChild(_title);
		}
		
		private function createInput():void
		{
			_input = new TextField();
			_input.addEventListener(MouseEvent.MOUSE_DOWN, onInputMouseDown);
			_input.addEventListener(KeyboardEvent.KEY_UP, onInputKeyUp);
			_input.border = true;
			_input.borderColor = 0x000000;
			_input.type = TextFieldType.INPUT;
			_input.multiline = false;
			_input.width = 480;
			_input.height = 30;
			
			var format:TextFormat = new TextFormat();
			format.align = TextFormatAlign.CENTER;
			format.size = 18;
			_input.defaultTextFormat = format;
			
			addChild(_input);
		}
		
		private function createButton():void
		{
			_button = new Sprite();
			_button.addEventListener(MouseEvent.CLICK, onClick);
			_button.graphics.lineStyle(1, 0x333333);
			_button.graphics.beginFill(0xFFFFFF);
			_button.graphics.drawRect(0, 0, 40, 30);
			_button.graphics.endFill();
			_button.mouseEnabled = true;
			_button.mouseChildren = false;
			_button.buttonMode = true;
			
			var txt:TextField = new TextField();
			txt.text = 'GO';
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.x = (_button.width - txt.width) * .5;
			txt.y = (_button.height - txt.height ) * .5;
			
			_button.addChild(txt);
			addChild(_button);
		}
		
		private function createOutput():void
		{
			_output = new TextField();
			_output.border = true;
			_output.borderColor = 0x333333;
			_output.type = TextFieldType.DYNAMIC;
			_output.multiline = true;
			_output.width = stage.stageWidth;
			_output.height = 200;
			_output.text = '';
			
			addChild(_output);
		}
		
		private function onInputKeyUp(e:KeyboardEvent):void 
		{
			if(e.keyCode == Keyboard.ENTER)
				_clickHandler();
		}
		
		private function onInputMouseDown(e:MouseEvent):void 
		{
			_input.setSelection(0, _input.length);
		}
		
		private function onClick(e:MouseEvent):void 
		{
			_clickHandler();
		}
		
		private var _title:TextField;
		private var _input:TextField;
		private var _button:Sprite;
		private var _output:TextField;
		private var _clickHandler:Function;
		
	}
	
}